<?php
/**
 * Description of Manager
 *
 * @author FFOZEU
 */
namespace Library;

if( !defined('IN') ) die('Hacking Attempt');

abstract class Manager {
    
    protected $dao;
    
    public function __construct($dao){
        $this->dao = $dao;
    }
    
    public function fecthAssoc_data($requete, $refObjet){
        
        $output = array();
        while ($data = $requete->fetch(\PDO::FETCH_ASSOC)){
            
            $output[] = new $refObjet($data);
            
        }
        $requete->closeCursor();
        
        return $output;
    }
    
    public function fecthRow_data($requete, $refObjet){
        $output ='';
        if($data = $requete->fetch(\PDO::FETCH_ASSOC)){
            
            $output = new $refObjet($data);
        }
        $requete->closeCursor();
        
        return $output;
    }
    
    public function delete($table, array $param, $jonction='AND'){
        $out='';
        $i=0;
        foreach ($param as $key => $value) {
            $out .=($i!=0?$jonction.' ':'').$key.'='.$value;
            $i++;
        }
        $sql ='DELETE 
               FROM '.$table.' 
               WHERE '.$out;
        return $this->dao->query($sql);
    }
    
    public function findById($table, $id){
        $sql = 'SELECT t.* 
                FROM '.$table.' as t
                WHERE t.id=:id';
        
        $req = $this->dao->prepare($sql);
        $req->bindValue(':id', intval($id));
        $req->execute();
        return $req;
        
    }
    
    public function updateRecord($table, array $param, $id, $jonction=','){
        $out='';
        $i=0;
        foreach ($param as $key => $value) {
            $out .=($i!=0?$jonction.' ':'').$key.'="'.(string)$value.'"';
            $i++;
        }
        $sql ='UPDATE '.$table.' as t
               SET '.$out.'
               WHERE t.id=:id';
        $req = $this->dao->prepare($sql);
        $req->bindValue(':id', intval($id));
        return $req->execute();
    }
}

?>
